<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ page import="java.util.Collection" %>
<%@ page import="java.time.format.DateTimeFormatter" %>
<%@ page import="com.gmono.managers.*" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.stream.Collectors" %>
<%@ page import="java.util.List" %>
<%@include file="../Login/loginCheck.jsp"%>
<%@include file="../Login/loginedUser.jsp"%>

<jsp:include page="header.jsp" flush="true">
  <jsp:param name="activeid" value="1"/>
</jsp:include>

<%
  Collection<Article> articles= ArticleManager.getArticles();
%>
<%
  ArrayList<Category> categories=Common.ResultToCollection(Common.Query("select * from category order by id"),
          row->Category.getObject(Category.class,row));
  Collection<Category> categories1=categories.stream().filter(c->c.getId()!=0).collect(Collectors.toList());
%>
    <div class="col-sm-9 col-sm-offset-3 col-md-10 col-lg-10 col-md-offset-2 main" id="main">
      <form action="../Article/checkAll.jsp" method="post" >
        <h1 class="page-header">操作</h1>
        <ol class="breadcrumb">
          <li><a href="add-article.jsp">增加文章</a></li>
        </ol>
        <h1 class="page-header">管理 <span class="badge">7</span></h1>
        <div class="table-responsive">
          <table class="table table-striped table-hover">
            <thead>
              <tr>
                <th><span class="glyphicon glyphicon-th-large"></span> <span class="visible-lg">选择</span></th>
                <th><span class="glyphicon glyphicon-file"></span> <span class="visible-lg">标题</span></th>
                <th><span class="glyphicon glyphicon-list"></span> <span class="visible-lg">栏目</span></th>
                <th class="hidden-sm"><span class="glyphicon glyphicon-tag"></span> <span class="visible-lg">标签</span></th>
                <th class="hidden-sm"><span class="glyphicon glyphicon-comment"></span> <span class="visible-lg">评论</span></th>
                <th><span class="glyphicon glyphicon-time"></span> <span class="visible-lg">日期</span></th>
                <th><span class="glyphicon glyphicon-pencil"></span> <span class="visible-lg">操作</span></th>
              </tr>
            </thead>
            <tbody>
            <%for(Article a:articles){%>
            <%
              int cid=a.getCategory();
              ArrayList<Category> nows=Common.ResultToCollection(Common.Query("select * from category where id=?",cid),
                      row->Category.getObject(Category.class,row));
              List<Category> now=nows.stream().filter(c->c.getId()!=0).collect(Collectors.toList());
              Category nowobj=null;
              if(now.size()==1)
              {
                nowobj=now.get(0);
              }
            %>
              <tr>
                <td><input type="checkbox" class="input-control" name="checkbox[]" value="" /></td>
                <td class="article-title"><%=a.getTitle()%></td>
                <td><%=nowobj==null? "未知栏目":nowobj.getName()%></td>
                <td class="hidden-sm">PHP、JavaScript</td>
                <td class="hidden-sm">0</td>
                <td><%=DateTimeFormatter.ofPattern("yyyy-MM-dd").format(a.getTime().toLocalDateTime())%></td>
                <td><a href="update-article.jsp?id=<%=a.getId()%>">修改</a> <a rel="<%=a.getId()%>">删除</a></td>
              </tr>
            <%}%>
            </tbody>
          </table>
        </div>
        <footer class="message_footer">
          <nav>
            <div class="btn-toolbar operation" role="toolbar">
              <div class="btn-group" role="group"> <a class="btn btn-default" onClick="select()">全选</a> <a class="btn btn-default" onClick="reverse()">反选</a> <a class="btn btn-default" onClick="noselect()">不选</a> </div>
              <div class="btn-group" role="group">
                <button type="submit" class="btn btn-default" data-toggle="tooltip" data-placement="bottom" title="删除全部选中" name="checkbox_delete">删除</button>
              </div>
            </div>
            <ul class="pagination pagenav">
              <li class="disabled"><a aria-label="Previous"> <span aria-hidden="true">&laquo;</span> </a> </li>
              <li class="active"><a href="#">1</a></li>
              <li><a href="#">2</a></li>
              <li><a href="#">3</a></li>
              <li><a href="#">4</a></li>
              <li><a href="#">5</a></li>
              <li><a href="#" aria-label="Next"> <span aria-hidden="true">&raquo;</span> </a> </li>
            </ul>
          </nav>
        </footer>
      </form>
    </div>






    <jsp:include page="foot.jsp" flush="true"/>

<script>
    //是否确认删除
    $(function(){
        $("#main table tbody tr td a").click(function(){
            var name = $(this);
            var id = name.attr("rel"); //对应id
            if (event.srcElement.outerText == "删除")
            {
                if(window.confirm("此操作不可逆，是否确认？"))
                {
                    $.ajax({
                        type: "GET",
                        url: "../Article/delete.jsp?id="+id,
                        cache: false, //不缓存此页面
                        success: function (data) {
                            window.location.reload();
                        }
                    });
                };
            };
        });
    });
</script>